//
//  MDGameViewController.swift
//  麦兜直播
//
//  Created by Apple on 16/10/17.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit

private let gameCellID = "gameCellID"
private let gameTitleID = "gameTitleID"

private let itemW = (screenWidth-2*margin)/3.0
private let seactionHeadH:CGFloat = 50
private let margin:CGFloat = 15
private let listViewH:CGFloat = 70

class MDGameViewController: MDAnimationController {
    
    fileprivate lazy var gameListView:MDGameListView = {[unowned self] in
        let listView = MDGameListView.show()
        listView.frame = CGRect(x: 0, y: -listViewH, width: screenWidth, height: listViewH)
        listView.backgroundColor = UIColor.yellow
        return listView
    }()
    
    fileprivate lazy var titleHeadView:MDTitleBarView = {[unowned self] in
        
        let headView = MDTitleBarView.show()
        headView.iconView.image = UIImage(named: "Img_orange")
        headView.title.text = "常用"
        headView.moreTitle.isHidden = true
        headView.frame = CGRect(x: 0, y: -(seactionHeadH+listViewH), width: screenWidth, height: seactionHeadH)
        return headView
        }()
    
    fileprivate lazy var collectionView:UICollectionView = {[unowned self] in
        let layout = UICollectionViewFlowLayout()
        let itemH = itemW*0.8
        layout.itemSize = CGSize(width:itemW , height: itemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.headerReferenceSize = CGSize(width: screenWidth, height: seactionHeadH)
        layout.sectionInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        collectionView.contentInset = UIEdgeInsets(top: seactionHeadH+listViewH, left: 0, bottom: 0, right: 0)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: "MDGameCell", bundle: nil), forCellWithReuseIdentifier: gameCellID)
        collectionView.register(UINib(nibName: "MDTitleBarView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: gameTitleID)
        return collectionView
        }()
    fileprivate lazy var gameNetTool:MDGameTool = MDGameTool()
    override func viewDidLoad() {
        super.viewDidLoad()
       setUpUI()
       requestData()
        
    }

}

extension MDGameViewController{
     override func setUpUI(){
        contentView = collectionView
        super.setUpUI()
        view.addSubview(collectionView)
        collectionView.addSubview(titleHeadView)
        collectionView.addSubview(gameListView)
        
    }
    
    
    fileprivate  func requestData() {
        
        gameNetTool.reuestData {
            self.stopAnimation()
            self.collectionView.reloadData()
            let games:[MDHomeGroup] = Array(self.gameNetTool.baseRoomGroup[0..<10])
            self.gameListView.homeGroup = games
        }
    }
}

// MARK:- 数据源和代理方法
extension MDGameViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return gameNetTool.baseRoomGroup.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gameCellID, for: indexPath) as!MDGameCell
            cell.homeGroup = gameNetTool.baseRoomGroup[indexPath.row]
            return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: gameTitleID, for: indexPath) as! MDTitleBarView
        headView.iconView.image = UIImage(named: "Img_orange")
        headView.title.text = "全部"
        headView.moreTitle.isHidden = true
        return headView
    }
    
}
